package com.miaomiaomiao.gegege.service;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jboss.logging.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.miaomiaomiao.gegege.bo.GeContent;


@Transactional
@Service("geService")
public class GeService {
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	private Logger log = Logger.getLogger(getClass());
	public void addGe(final GeContent g) {
		String sql = "insert into tb_gege (dtime, content) values (?,?)";
		log.info("geggege" + g.getContent() + g.getDate());
		
		jdbcTemplate.update(sql, new PreparedStatementSetter() {
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setString(2, g.getContent());
				Timestamp t = new Timestamp(g.getDate().getTime());
				ps.setTimestamp(1, t);
			}
		});
	}
	
	public List<Map<String,Object>> showGe() {
		final String sql = "select id, content, dtime from tb_gege";
		List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
}
